<?php
namespace App\Services;

use NcJoes\OfficeConverter\OfficeConverter;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;

class PdfService extends BasicService
{
    // use tcpdf transfer word to pdf
    // drawback: cause style less, chinese show ???, libreoffice makes better transfer
    public static function wordToPdf(string $wordPath)
    {
        $ext = pathinfo($wordPath, PATHINFO_EXTENSION);
        if (!in_array($ext, ['doc', 'docx'])) {
            return self::error('仅支持doc和docx格式的文件转为pdf');
        }
        if (!is_file($wordPath)) {
            return self::error('word文件不存在');
        }
        $pdfPath = BASE_PATH . '/vendor/tecnickcom/tcpdf';
        Settings::setPdfRendererPath($pdfPath);
        Settings::setPdfRendererName('TCPDF');
        
        $phpword = IoFactory::load($wordPath);
        // $pdfWriter = IOFactory::createWriter($phpword, 'PDF');
        $pdfWriter = IoFactory::createWriter($phpword, 'PDF');
        $pdfWriter->setFont('droidsansfallback', '', 20);
        $r = $pdfWriter->save('test.pdf');
        return self::success($r);
    }

    public static function word2PdfByLibreoffice($word, $outdir = 'public/uploads')
    {
        $cmd = 'soffice --headless --convert-to pdf --outdir ' . escapeshellarg($outdir) . ' ' . escapeshellarg($word);
        // 借助 libreoffice docker 命令转换
        // 需要先将文件 copy 到 宿主机和 docker 共享的目录中， 假设将宿主机的 /usr/local/docker/libreoffice/config 映射到 
        $isLinuxSys = PHP_OS == 'Linux';
        if ($isLinuxSys) {
            //todo need to change to your own path
            $shareVolume = \hyperf\config\config('pdf_convert.libreoffice_in_docker.host_share_volume'); # '/usr/local/docker/libreoffice/config';
            $dockerVolume = \hyperf\config\config('pdf_convert.libreoffice_in_docker.docker_volume');#'/config';
            copy($word, $shareVolume . '/' . basename($word));

            $cmd = sprintf('docker exec -it libreoffice /usr/bin/soffice --headless --convert-to pdf --outdir %s %s/%s', $dockerVolume, $dockerVolume, basename($word));
        } 

        $output = null;
        $retval = null;
        exec($cmd, $output, $retval);

        $pdfName = pathinfo(basename($word), PATHINFO_FILENAME) . '.pdf';
        $targetPath = rtrim($outdir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $pdfName;

        if ($isLinuxSys) {
            copy($shareVolume . '/' . $pdfName, $targetPath);
            unlink($shareVolume . '/' . basename($word));
            unlink($shareVolume . '/' . $pdfName);
        }

        // echo $targetPath;
        // var_dump($retval, file_exists($targetPath));
        if ($retval === 0 && file_exists($targetPath)) {
            return $targetPath;
        }
        return false;
    }

    // use OfficeConverter library to transfer word to pdf. supports also ppt, excel and so on
    // @see https://github.com/ncjoes/office-converter
    // issue https://github.com/PHPOffice/PHPWord/issues/1840
    public static function word2PdfByConverter($word, $outdir = 'public/uploads')
    {
        $converter = new OfficeConverter($word, $outdir, 'soffice');
        $pdfName = pathinfo(basename($word), PATHINFO_FILENAME) . '.pdf';;
        $res = $converter->convertTo($pdfName);
        if ($res) {
            return $res;
        } else {
            return false;
        }
    }
}